/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import pe.gov.mtc.dgac.licencias.bean.ClienteTO;
import pe.gov.mtc.dgac.licencias.service.ClienteService;
import pe.gov.mtc.dgac.licencias.struts.Constants;
import pe.gov.mtc.dgac.licencias.util.Page;

/**
 *
 * @author externo.gamaro
 */
public class ClienteAction extends DispatchAction{
    private final static String SUCCESS = "success";
    private Log logger = LogFactory.getLog(this.getClass());
    private ClienteService cliService;
    
    public ClienteAction(ClienteService cliService) {
        super();
        this.cliService = cliService;
        
    }
    
    public ActionForward buscarCliente(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("buscarCliente");
        String codcli = req.getParameter("cliente");
        String numdoc = req.getParameter("txtnumdoc");
        String clinom = req.getParameter("txtnombres");
        String cliapepat = req.getParameter("txtapepat");
        String cliapemat = req.getParameter("txtapemat");
        String page=req.getParameter("page");
        HttpSession sesion = req.getSession();
        boolean nuevabusqueda=false;
     
          if(page.equals(""))page="1"; 
          if(page.equals("0"))nuevabusqueda=true;
              
              int pagina=Integer.parseInt(page);
              if(nuevabusqueda) {
                 ClienteTO cli=new ClienteTO();
                 cli.setCodCliente(0L);
                 if (cliapepat!=null) cli.setApellidoPat(cliapepat);
                 if (cliapemat!=null) cli.setApellidoMat(cliapemat);
                 if (clinom!=null) cli.setNombres(clinom);
                 if (!codcli.equals("")) cli.setCodCliente(Long.parseLong(codcli));
                 if (numdoc!=null) cli.setNumIdDoc(numdoc);
                 System.out.println(cliapepat+" "+cliapemat);
                 //Page pageClientes=climgr.filtraCliente(cli,pagina,Page.SIZE_PAGE);
                 Page pageClientes=cliService.buscarCliente(cli, pagina,Page.SIZE_PAGE);
     
                 sesion.setAttribute("listClientes", pageClientes);
                 sesion.setAttribute("cli", cli);
              }else{
                ClienteTO cli=(ClienteTO)sesion.getAttribute("cli");
                //Page pageClientes=climgr.filtraCliente(cli,pagina,Page.SIZE_PAGE);
                Page pageClientes=cliService.buscarCliente(cli, pagina,Page.SIZE_PAGE);
                sesion.setAttribute("listClientes", pageClientes);
              }
              
        return mapping.findForward(Constants.SUCCESS.getValue());
        
    }
}
